package com.example.administrator.managesystem.activity.login;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.activity.VideoListActivity;
import com.example.administrator.managesystem.base.BaseActivity;
import com.example.administrator.managesystem.bean.BaseEvent;
import com.example.administrator.managesystem.presenter.login.LoginPresenter;
import com.example.administrator.managesystem.ui.LoginView;
import com.example.administrator.managesystem.utils.ActivityUtils;
import com.example.administrator.managesystem.utils.ToastUtils;
import com.example.administrator.managesystem.video.LandListActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by TR-AND2 on 2017/12/8.
 */

public class LoginActivity extends BaseActivity implements LoginView {

    private EditText userName, password;
    private TextView password_forget, login_tv, login_one_shot,register;


    @Override
    public void initView() {
        showfloatButton(false, false);
        EventBus.getDefault().register(this);
        userName = findViewById(R.id.userName);
        password = findViewById(R.id.password);
        login_tv = findViewById(R.id.login_tv);
        password_forget = findViewById(R.id.password_forget);
        login_one_shot = findViewById(R.id.login_one_shot);
        register=findViewById(R.id.register);
    }

    @Override
    public void doBusiness() {

        login_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showProgressDialog();
                new LoginPresenter(LoginActivity.this).loginRequest(userName.getText().toString(), password.getText().toString(), LoginActivity.this);
            }
        });

        //忘记密码
        password_forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoActivity(ForgetPsdActivity.class, false);
            }
        });
        login_one_shot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoActivity(OneShotLoginActivity.class, false);
            }
        });
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             gotoActivity(RegisterActivity.class,false);
            }
        });

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
              ActivityUtils.getInstance().exitSystem();
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }


    @Override
    public void showMessage(String msg) {
        dismissProgressDialog();
    }

    @Override
    public void navigateToHome() {
        dismissProgressDialog();
        gotoActivity(LandListActivity.class, true);
    }

    @Override
    public void startCountDown() {

    }


    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onEventReceived(BaseEvent event) {
        if(event.getCode() == 0){
            finish();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
      EventBus.getDefault().unregister(this);
    }
}

